package com.banggood.upms.web;

import com.banggood.silk.web.api.common.PagedResult;
import com.banggood.silk.web.api.exception.SkbException;
import com.banggood.upms.bean.exception.ExceptionEnum;
import com.banggood.upms.bean.po.UserScope;
import com.banggood.upms.bean.vo.search.BaseSearchCriteria;
import com.banggood.upms.bean.vo.validation.AddGroup;
import com.banggood.upms.bean.vo.validation.SearchGroup;
import com.banggood.upms.bean.vo.validation.UpdateGroup;
import com.banggood.upms.common.constant.UpmsConstant;
import com.banggood.upms.service.UserScopeService;
import com.banggood.upms.common.component.CommonComponent;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *
 * @author XuChuangFeng
 * @date 2017/10/13
 */
@RestController
@RequestMapping("/scope")
public class ScopeController {

    @Autowired
    private CommonComponent commonComponent;

    @Autowired
    private UserScopeService userScopeService;

    /**
     * 搜索用户域
     * @param criteria
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ApiOperation(value = "搜索用户域", notes = "搜索用户域", produces = MediaType.APPLICATION_JSON_VALUE)
    public PagedResult<UserScope> selectUserScope(@Validated({SearchGroup.class}) BaseSearchCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getRows());
        List<UserScope> userScopes = userScopeService.selectByCriteria(criteria);
        long total = new PageInfo<>(userScopes).getTotal();
        PagedResult<UserScope> result = new PagedResult<>(userScopes, total);
        return result;
    }

    @RequestMapping(value = "/select-all", method = RequestMethod.GET)
    @ApiOperation(value = "搜索所有用户域", notes = "搜索所有用户域", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<UserScope> selectAll() {
        return userScopeService.selectAll();
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "新增用户域", notes = "新增用户域", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "scopeKey", value = "域的键", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, paramType = "query", dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 1006, message = "用户域已经存在!"),
            @ApiResponse(code = 5000, message = "操作失败!")
    })
    public String insertUserScope(@Validated({AddGroup.class}) UserScope userScope) throws Exception {
        userScope.setScopeId(commonComponent.generateId());
        int count = userScopeService.insert(userScope);
        if(count != 1) {
            throw new SkbException(ExceptionEnum.OPERATION_FAILED.getCode(), ExceptionEnum.OPERATION_FAILED.getMessage());
        }
        return UpmsConstant.SUCCESS;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "更新用户域", notes = "更新用户域,只允许更新备注，不会更新key", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "scopeId", value = "主键", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, paramType = "query", dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 5000, message = "操作失败!")
    })
    public String updateByPrimaryKey(@Validated({UpdateGroup.class}) UserScope userScope) throws Exception {
        int count = userScopeService.updateByPrimaryKey(userScope);
        if(count != 1) {
            throw new SkbException(ExceptionEnum.OPERATION_FAILED.getCode(), ExceptionEnum.OPERATION_FAILED.getMessage());
        }
        return UpmsConstant.SUCCESS;
    }
}
